//
//  ZSStatusOriginalView.swift
//  ZSWeibo
//
//  Created by zsj1992 on 16/4/12.
//  Copyright © 2016年 ichange. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

let ZSStatusCellMargin = 5

class ZSStatusOriginalView: UIView {

    

    
    var status:ZSStatus?{
    
    
        didSet{

            iconView.sd_setImageWithURL(NSURL(string: (status?.user?.profile_image_url)!))
            
            let imgName = "common_icon_membership_level\(status?.user?.mbrank)"
            
            mbRankImageView.image = UIImage(named: imgName)
            
            
            /// 认证类型 -1：没有认证，1：认证用户 avatar_vip，2,3,5: 企业认证 avatar_enterprise_vip，220: 达人 avatar_grassroot
            
            let x = status?.user?.verified_type
            
            var name:String = ""
            
            if x == -1{
                name = ""
            }
            else if (x == 2 || x==3 || x==5){
                name = "avatar_enterprise_vip"
            }
            else if (x==1){
                name = "avatar_vip"
            }
            else{
                name = "avatar_grassroot"
            }
            
            
            
            verifiedImageView.image = UIImage(named: name)
            
            nameLabel.text = status?.text
            
            
            timeLabel.text = status?.created_at
            
            sourceLabel.text = status?.source
            
            contentLabel.text = status?.text
            
        }
    }
    
    

    
    
    
    
    
    
    
    
    /*懒加载子控件*/
    /// 用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    /// 用户等级
    private lazy var mbRankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    /// 用户认证类型
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    /// 用户名
    private lazy var nameLabel: UILabel = UILabel(text: "15期的童鞋们", textColor: UIColor.darkGrayColor(), fontSize: 15)
    /// 微博时间
    private lazy var timeLabel: UILabel = UILabel(text: "15:15", textColor: UIColor.orangeColor(), fontSize: 12)
    /// 微博来源
    private lazy var sourceLabel: UILabel = UILabel(text: "隔壁老王", textColor: UIColor.darkGrayColor(), fontSize: 12)
    /// 微博正文
    private lazy var contentLabel: UILabel = UILabel(text: "我没有男胖呀我没有男胖呀我没有男胖呀我没有男胖呀我没有男胖呀我没有男胖呀我没有男胖呀我没有男胖呀我没有男胖呀", textColor: UIColor.darkGrayColor(), fontSize: 15)
    
  
    
    /**重写构造方法*/
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        
    setupUI()
    print("sssssssss")
        print(frame)
        print("sssssssss")

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}














extension ZSStatusOriginalView{

    func setupUI(){
    //添加所有子控件
    addSubview(iconView)
    addSubview(mbRankImageView)
    addSubview(verifiedImageView)
    addSubview(timeLabel)
    addSubview(nameLabel)
    addSubview(sourceLabel)
    addSubview(contentLabel)
    
    //添加约束
    
    //头像
    iconView.snp_makeConstraints { (make) -> Void in

        iconView.layer.cornerRadius = 20
        make.left.top.equalTo(self).offset(ZSStatusCellMargin)
        make.size.equalTo(CGSizeMake(40, 40))
        
        
        }
    
        //名字
    nameLabel.snp_makeConstraints { (make) -> Void in
        
        make.left.equalTo(iconView.snp_right).offset(ZSStatusCellMargin)
     
        make.top.equalTo(iconView.snp_top)
        }
        
    //皇冠等级
    mbRankImageView.snp_makeConstraints { (make) -> Void in
        
        make.top.equalTo(nameLabel.snp_top)
        
        make.left.equalTo(nameLabel.snp_right).offset(ZSStatusCellMargin)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_left)
            make.bottom.equalTo(iconView.snp_bottom)
        }
    //认证类型
    verifiedImageView.snp_makeConstraints { (make) -> Void in
        
        make.right.equalTo(iconView.snp_right)
        
        make.bottom.equalTo(iconView.snp_bottom)
        
        }
    
     //状态来源
    sourceLabel.snp_makeConstraints { (make) -> Void in
        
        make.left.equalTo(timeLabel.snp_right).offset(ZSStatusCellMargin)
        make.bottom.equalTo(timeLabel.snp_bottom)
        
        }
        
    //正文
    contentLabel.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(iconView.snp_bottom).offset(ZSStatusCellMargin)
        make.left.equalTo(self).offset(ZSStatusCellMargin)
        
        contentLabel.numberOfLines = 0
        contentLabel.preferredMaxLayoutWidth = screenWidth - CGFloat(2*ZSStatusCellMargin)
        }

    }
}











